package cn.txm.pet.controller;

import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.PageList;
import cn.txm.basic.util.RedisUtils;
import cn.txm.org.domain.Employee;
import cn.txm.pet.domain.Adopt;
import cn.txm.pet.query.AdoptQuery;
import cn.txm.pet.service.IAdoptService;
import cn.txm.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class AdoptController {
    @Autowired
    private IAdoptService adoptService;

    /**
     * 前台用户发布宠物寻主消息
     * @param adopt
     * @return
     */
    @PutMapping("/adopt")
    public AjaxResult publishAdopt(@RequestBody Adopt adopt, HttpServletRequest request){
        try {
            //1.获取当前用户
            User user = RedisUtils.INSTANCE.getUser(request, User.class);
            System.out.println(user);
            adopt.setUser_id(user.getId());
            adoptService.publishAdopt(adopt);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("提交失败,我们真正殴打程序员"+e.getMessage());
        }
    }

    /**
     * 后动态管理，分页查询宠物寻主消息
     * @param adoptQuery
     * @return
     */
    @PostMapping("/adopt")
    public PageList<Adopt> pageList(@RequestBody AdoptQuery adoptQuery,HttpServletRequest request){
        Employee employee =RedisUtils.INSTANCE.getUser(request,Employee.class);
        adoptQuery.setShop_id(employee.getShop().getId());
        return  adoptService.queryPage(adoptQuery);
    }

    /**
     * 后台管理：商家后台处理寻主消息
     * @param id
     * @param handleResult
     * @return
     */
    @GetMapping("/adopt/{id}/{handleResult}")
    public AjaxResult handleAdopt(@PathVariable("id") Long id,@PathVariable("handleResult")Integer handleResult){
        try {
            adoptService.handleAdopt(id,handleResult);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("处理失败，我们正在殴打程序员"+e.getMessage());
        }
    }
}
